import Vapor
import Fluent

struct AdvancedBuildingController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        let userRoute = routes.grouped("api", "advancedbuilding")
        userRoute.post(use: createHandler)
        userRoute.get(use: getAllHandler)
        userRoute.put(use: updateHandler)
        userRoute.delete(use: deleteHandler)
        userRoute.get(":ownMailAddess", use: getHandlerByOwnEmailAdress)


    }
    
    func createHandler(_ req: Request) throws -> EventLoopFuture<AdvancedBuilding>  {
        let advancedBuilding = try req.content.decode(AdvancedBuilding.self)
        return advancedBuilding.save(on: req.db).map { advancedBuilding }
    }
    
    func getAllHandler(_ req: Request) throws -> EventLoopFuture<[AdvancedBuilding]> {
        AdvancedBuilding.query(on:req.db).all()
    }
    
    func getHandlerByOwnEmailAdress(_ req: Request) throws -> EventLoopFuture<[AdvancedBuilding]> {
        let ownMailAddess = req.parameters.get("ownMailAddess")!
        let ownMailAddessTemp = "\(ownMailAddess)"
        return AdvancedBuilding.query(on: req.db).filter(\.$ownersEMailAddress == ownMailAddessTemp).all()
        
    }
    
    func updateHandler(_ req: Request) throws -> EventLoopFuture<AdvancedBuilding> {
        let data = try req.content.decode(AdvancedBuilding.self)
        return AdvancedBuilding.find(data.id, on: req.db).unwrap(or: Abort(.notFound)).flatMap { advancedBuilding in
            advancedBuilding.ownersEMailAddress = data.ownersEMailAddress
            advancedBuilding.demo = data.demo
            advancedBuilding.tag = data.tag
            advancedBuilding.customLogoID = data.customLogoID
            advancedBuilding.positionX = data.positionX
            advancedBuilding.positionY = data.positionY
            advancedBuilding.name = data.name
            advancedBuilding.width = data.width
            advancedBuilding.length = data.length
            advancedBuilding.height = data.height
            advancedBuilding.pitch = data.pitch
            advancedBuilding.wallTickness = data.wallTickness
            advancedBuilding.floorOverlappingBuildingSize = data.floorOverlappingBuildingSize
            advancedBuilding.floorTickness = data.floorTickness
            advancedBuilding.roofTickness = data.roofTickness
            advancedBuilding.roofOverhangSide = data.roofOverhangSide
            advancedBuilding.roofOverhangFrontRear = data.roofOverhangFrontRear
            advancedBuilding.highPressureCooling = data.highPressureCooling
            advancedBuilding.distanceWallHeaters = data.distanceWallHeaters
            advancedBuilding.sideModeExhaustSystemEnumInt = data.sideModeExhaustSystemEnumInt
            advancedBuilding.ventilationSystemEnumInt = data.ventilationSystemEnumInt
            advancedBuilding.sideDogHouseLength = data.sideDogHouseLength
            advancedBuilding.numberOfHeaters = data.numberOfHeaters
            advancedBuilding.numberOfInlets = data.numberOfInlets
            advancedBuilding.dogHouseSide = data.dogHouseSide
            advancedBuilding.dogHouseFront = data.dogHouseFront
            advancedBuilding.numberOfFansSide = data.numberOfFansSide
            advancedBuilding.numberOfFansEnd = data.numberOfFansEnd
            advancedBuilding.numberOfChimneys = data.numberOfChimneys
            advancedBuilding.numberOfRowsOfInlets = data.numberOfRowsOfInlets
            advancedBuilding.tunnelAirspeed = data.tunnelAirspeed
            advancedBuilding.animalTypeEnumInt = data.animalTypeEnumInt
            advancedBuilding.animalWeight = data.animalWeight
            advancedBuilding.numberAnimals = data.numberAnimals
            advancedBuilding.numberOfFeedingBins = data.numberOfFeedingBins
            advancedBuilding.controlPanelSupplier = data.controlPanelSupplier
            advancedBuilding.serviceRoom = data.serviceRoom
            advancedBuilding.serviceRoomHeight = data.serviceRoomHeight
            advancedBuilding.serviceRoomWidth = data.serviceRoomWidth
            advancedBuilding.serviceRoomLength = data.serviceRoomLength
            advancedBuilding.serviceRoomDoorHeight = data.serviceRoomDoorHeight
            advancedBuilding.cages_cages = data.cages_cages
            advancedBuilding.cages_name = data.cages_name
            advancedBuilding.cages_sectionLength = data.cages_sectionLength
            advancedBuilding.cages_sectionWidth = data.cages_sectionWidth
            advancedBuilding.cages_sectionHeight = data.cages_sectionHeight
            advancedBuilding.cages_startLength = data.cages_startLength
            advancedBuilding.cages_endLength = data.cages_endLength
            advancedBuilding.cages_bottomHeight = data.cages_bottomHeight
            advancedBuilding.cages_extensionDueToCatwalkPartHeight = data.cages_extensionDueToCatwalkPartHeight
            advancedBuilding.cages_catWalkHeigth = data.cages_catWalkHeigth
            advancedBuilding.cages_startPositionCagesIntoTheWidth = data.cages_startPositionCagesIntoTheWidth
            advancedBuilding.cages_startPositionCagesIntoTheLength = data.cages_startPositionCagesIntoTheLength
            advancedBuilding.cages_NumberOfSections = data.cages_NumberOfSections
            advancedBuilding.cages_NumberOfTiersRow01 = data.cages_NumberOfTiersRow01
            advancedBuilding.cages_NumberOfTiersRow02 = data.cages_NumberOfTiersRow02
            advancedBuilding.cages_NumberOfTiersRow03 = data.cages_NumberOfTiersRow03
            advancedBuilding.cages_NumberOfTiersRow04 = data.cages_NumberOfTiersRow04
            advancedBuilding.cages_NumberOfTiersRow05 = data.cages_NumberOfTiersRow05
            advancedBuilding.cages_NumberOfTiersRow06 = data.cages_NumberOfTiersRow06
            advancedBuilding.cages_NumberOfTiersRow07 = data.cages_NumberOfTiersRow07
            advancedBuilding.cages_NumberOfTiersRow08 = data.cages_NumberOfTiersRow08
            advancedBuilding.cages_NumberOfTiersRow09 = data.cages_NumberOfTiersRow09
            advancedBuilding.cages_NumberOfTiersRow10 = data.cages_NumberOfTiersRow10
            advancedBuilding.cages_NumberOfTiersRow11 = data.cages_NumberOfTiersRow11
            advancedBuilding.cages_NumberOfTiersRow12 = data.cages_NumberOfTiersRow12
            advancedBuilding.cages_catwalk1AboveTier = data.cages_catwalk1AboveTier
            advancedBuilding.cages_catwalk2AboveTier = data.cages_catwalk2AboveTier
            return advancedBuilding.save(on: req.db).map { return advancedBuilding }

        }
    }
    
    func deleteHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
        let data = try req.content.decode(AdvancedBuilding.self)
        return AdvancedBuilding.find(data.id, on: req.db).unwrap(or: Abort(.notFound)).flatMap { advancedBuilding in
            advancedBuilding.delete(on: req.db).transform(to: .noContent)
        }

    }

}
